package com.isolate.run;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.exec.CommandLine;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 代码编译器入口接口
 *
 * @author hammy
 */
@Slf4j
@Component
public class EditorEntryHandler extends TextWebSocketHandler {

    private static final ConcurrentHashMap<WebSocketSession, HammyThread> THREAD_MAP = new ConcurrentHashMap<>();

    /**
     * 关闭连接后的处理
     *
     * @param wsSession
     * @param status
     * @throws Exception
     */
    @Override
    public void afterConnectionClosed(WebSocketSession wsSession, CloseStatus status) throws Exception {
        super.afterConnectionClosed(wsSession, status);
    }

    /**
     * 建立socket连接
     *
     * @param wsSession
     * @throws Exception
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession wsSession) throws Exception {
        super.afterConnectionEstablished(wsSession);
    }

    /**
     * 接收并处理前端(xterm js)发送过来的消息
     *
     * @param wsSession
     * @param message
     * @throws Exception
     */
    @Override
    public void handleMessage(WebSocketSession wsSession, WebSocketMessage<?> message) {
        HammyThread hammyThread = THREAD_MAP.getOrDefault(wsSession, new HammyThread(wsSession, message));
        hammyThread.execute("/usr/local/Cellar/python/3.7.4/bin/python3.7","/Users/koutadatomi/python/test.py");

    }
}
